package com.icss.train_sys_back.mapper.train_manage;

import com.icss.train_sys_back.entity.ticket_manage.TicketNumberEntity;
import com.icss.train_sys_back.entity.train_manage.StationEntity;
import com.icss.train_sys_back.entity.train_manage.TrainNumberEntity;
import com.icss.train_sys_back.entity.train_manage.TrainStationEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author wk
 * @desc
 * @date 2025/9/25 16:44
 */
@Mapper
public interface TrainNumberMapper {

    //同步获取车次列表和车次经停站列表

       /* @Results(
                @Result(
                        column = "id", // 一对多中，一这个表要关联多这个表的查询需要的字段
                        property = "trainStationEntities",//一这个实体类中包含的多这个对象的实体类中定义的字段
                        javaType = List.class,//类型
                        many = @Many(select = "") // 关联查询
                )
        )*/
    List<TrainNumberEntity> findTrainNumberList(String trainNumber, Integer status);

    @Update("update t_train_number set train_number=#{trainNumber},start_station=#{startStation},end_station=#{endStation},arrive_station_num=#{arriveStationNum},status=#{status},start_time=#{startTime},arrive_time=#{arriveTime},update_time=now() where id=#{id}")
    boolean updateTrainNumber(TrainNumberEntity trainNumber);

    @Insert("insert into t_train_number values(null,#{trainNumber},#{startStation},#{endStation},#{arriveStationNum},#{status},#{startTime},#{arriveTime},now(),now())")
    boolean addTrainNumber(TrainNumberEntity trainNumber);

    @Delete("delete from t_train_number where id = #{id}")
    boolean deleteTrainNumber(Integer id);

    //查找车站列表(下拉框显示)
    @Select("select * from t_station")
    List<StationEntity> findStationList();

    //-----------------------------------------------------
    //根据trainNumber删除经停站表的所有数据
    @Delete("delete  from t_train_station where train_number = #{trainnumber}")
    boolean deleteArriveStationByTrainNumber(String trainNumber);

    //根据车次Id获取车次实体，删除车次经停站表需要车次中的车辆号train Number
    @Select("select * from t_train_number where id = #{id}")
    TrainNumberEntity findTrainNumberByTrainId(Integer id);

    //根据车站名获取到车站的经纬度信息  -- X
    @Select("select global_x from t_station where station_name = #{stationName}")
    Integer findStationXByStationName(String stationName);

    //根据车站名获取到车站的经纬度信息  -- Y
    @Select("select global_y from t_station where station_name = #{stationName}")
    Integer findStationYByStationName(String stationName);

    //2.2 根据车次的始发站名称获取该车次的始发站 -- 获取始发站的经纬度用
    @Select("select * from t_station where station_name = #{startStation}")
    StationEntity findStartStationByStartStationName(String startStation);

    //经停站插入经停站表
    @Insert("insert into t_train_station values (null,#{trainNumberId},#{trainNumber},#{stationName},#{stationOrder},#{arriveTime}," +
            "#{departureTime},#{stayDuration},#{distanceFromStart},now())")
    void insertTrainStation(TrainStationEntity trainStation);

    //2.4 更新trainNumber表中 经停站数 字段 （传回来的经停站集合的长度）
    @Update("update t_train_number set arrive_station_num = #{size} where id = #{id} ")
    void updateTrainNumberStayStationNumber(int size, Integer id);
    //-----------------------------------------------------

    //根据车次Id获取经停站列表
    @Select("select * from t_train_station where train_number_id = #{trainNumberId} order by station_order ")
    List<TrainStationEntity> findTrainStationsByTrainNumberId(Integer trainNumberId);

    //根据id删除车次对应的经停站表信息
    @Delete("delete from t_train_station where train_number_id = #{id}")
    void deleteTrainStationByTrainNumberId(Integer id);

    @Select("select * from t_train_number")
    List<TrainNumberEntity> findAllTrainNumberList();

    //根据车辆号获取票数信息
    @Select("select count(*) as allNumbers , count(case when status = 0 then 1 end) as remainNumbers " +
            "from t_seat where train_number = #{trainNumber} and seat_type = #{carriageType} group by train_number")
    TicketNumberEntity getTicketNumberByTrainNumber(String trainNumber, Integer carriageType);

    //车次添加脚本
    void batchAdd(List<TrainNumberEntity> trainNumberEntityList);
}
